package com.yanqu.road.server.cmd.unioncampwar.cross;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogUnionCampWarOpenBox;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.unionCampWar.UnionCampWar;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.gameplayer.module.unioncampwar.UnionCampWarModule;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import org.apache.regexp.RE;


@Cmd(code = GameProtocol.S_UNION_CAMP_WAR_OPEN_BOX_CROSS_BACK, desc = "商帮宝库开箱返回")
public class CrossUnionCampOpenBoxBackCmd extends AbstractCommand {
    UnionCampWar.UnionCampWarOpenBoxRespMsg.Builder respMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        respMsg = UnionCampWar.UnionCampWarOpenBoxRespMsg.parseFrom(packet.getBytes()).toBuilder();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        Property cost = ConfigMgr.getUnionCampWarConfig().getLotteryCost();
        player.getModule(PreReduceModule.class).restorePreReduce(cost);

        if (respMsg.getRet() == 0) {
            if (!player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.UnionCampWar, eLogMoneyType.UnionCampWarOpenBoxCost)) {
                ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                return;
            }

            String rewards = respMsg.getRewards();

            player.getModule(CurrencyModule.class).addCurrency(rewards, eLogMoneyType.UnionCampWar, eLogMoneyType.UnionCampWarOpenBoxReward);

            int thisWeekId = GameConfig.getSystemThisWeekId();
            AutoLogMgr.add(new LogUnionCampWarOpenBox(thisWeekId, player.getUserId(),
                    respMsg.getGroupId(), respMsg.getCampId(), respMsg.getBoxId(), respMsg.getBoxRound(), respMsg.getIsBigReward(), respMsg.getRewards()));
        }

        player.sendPacket(ClientProtocol.U_UNION_CAMP_WAR_OPEN_BOX, respMsg);



    }

    @Override
    public void sendData(GamePlayer player) {
        if (null == player) {
            return;
        }

    }
}
